﻿/*---------------------------------------------------------------------*/
// fileName="LogCommon.cs" CompanyName="SureKAM"
// Copyright SureKAM (c). All right reserved.
/*---------------------------------------------------------------------*/

namespace SureKAM.BackUp.Notify.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    public class LogCommon
    {
        #region 私有字段
        private readonly static string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "Log\\Log.txt";
        private static FileStream filestream;
        private static LogCommon locm = new LogCommon();
        private static string LogStr;
        #endregion

        #region 构造
        #endregion

        #region 私有字段
        /// <summary>
        /// 打开志日文件
        /// </summary>
        private static bool open()
        {
            if (File.Exists(logFilePath))
            {
                try
                {
                    filestream = new FileStream(logFilePath, FileMode.Open, FileAccess.ReadWrite);
                    return true;
                }
                catch (Exception)
                {


                }
            }
            return false;
        }

        /// <summary>
        /// 关闭日志文件
        /// </summary>
        private static void close()
        {
            if (filestream != null)
            {
                filestream.Close();
            }
        }
        #endregion

        #region 属性
        #endregion

        #region 公有方法
        /// <summary>
        /// 读取Log文件
        /// </summary>
        /// <returns>返回读取信息</returns>
        public static string Read()
        {
            lock (locm)
            {
                if (String.IsNullOrEmpty(LogStr))
                {
                    if (!open()) return string.Empty;
                    string result = string.Empty;
                    StreamReader streamReader = new StreamReader(filestream);
                    try
                    {
                        result = streamReader.ReadToEnd();
                    }
                    catch (Exception)
                    {
                        result = string.Empty;
                    }
                    finally
                    {
                        streamReader.Close();
                        close();
                    }
                    LogStr = result;
                }
                return LogStr;
            }
        }

        /// <summary>
        /// 添加目志记录
        /// </summary>
        /// <param name="str">消息</param>
        /// <returns>返回True 成功  或 Flase 失败</returns>
        public static bool Write(string str)
        {
            lock (locm)
            {
                bool operation = false;
                if (!string.IsNullOrEmpty(str))
                {
                    if (!open()) return false;
                    StreamWriter streamWriter = new StreamWriter(filestream);
                    try
                    {
                        streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                        streamWriter.WriteLine(str);
                    }
                    catch (Exception)
                    {
                        operation = false;
                    }
                    finally
                    {
                        streamWriter.Flush();
                        streamWriter.Close();
                        close();
                    }
                    LogStr = null;
                }
                return false;
            }
        }

        /// <summary>
        /// 清空记录
        /// </summary>
        public static void Clear()
        {

            if (!open()) return;
            try
            {
                filestream.SetLength(0);
                LogStr = null;
            }
            catch (Exception)
            {

            }
            finally
            {
                close();
            }

        }
        #endregion
    }
}
